package com.dhh.toodledo.activity;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhh.toodledo.R;
import com.dhh.toodledo.exception.InValidAccountException;
import com.dhh.toodledo.model.Task;
import com.dhh.toodledo.util.AuthUtil;
import com.dhh.toodledo.util.CUtil;
import com.dhh.toodledo.util.HttpUtil;
import com.flurry.android.FlurryAgent;

import greendroid.app.GDActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ShowDueDateActivity extends GDActivity implements View.OnClickListener{
	private AuthUtil au;
	private Map<String,Integer> sumMap = new HashMap<String,Integer>();
	private List<Task> tasks;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setActionBarContentView(R.layout.duedate);
        au = new AuthUtil(getSharedPreferences("myPre", 0));
        sumMap.put("overdue", 0);
        sumMap.put("today", 0);
        sumMap.put("tomorrow", 0);
        sumMap.put("next7", 0);
        sumMap.put("later", 0);
        sumMap.put("nodue", 0);
        initActionBar();
        initView();
        new SearchThread().start();
	}
	
	@Override
    public void onStart(){
       super.onStart();
       FlurryAgent.onStartSession(this, "2ND7X5T2HQCRXGXJ1BUF");
    }
    
    @Override
    public void onStop(){
       super.onStop();
       FlurryAgent.onEndSession(this);
    }
	
	@Override
	public void onClick(View view) {
		Intent intent = new Intent(ShowDueDateActivity.this,ShowAllTaksActivity.class);
		if(view.getId()==R.id.dd_overdue_lst){
			intent.putExtra("showType", "overdue");
		}else if(view.getId()==R.id.dd_today_list){
			intent.putExtra("showType", "today");
		}else if(view.getId()==R.id.dd_tomorrow_lst){
			intent.putExtra("showType", "tomorrow");
		}else if(view.getId()==R.id.dd_next7_lst){
			intent.putExtra("showType", "next7");
		}else if(view.getId()==R.id.dd_later_lst){
			intent.putExtra("showType", "later");
		}else if(view.getId()==R.id.dd_nodue_lst){
			intent.putExtra("showType", "nodue");
		}
		this.startActivity(intent);
	}
	
	public void initActionBar(){
		getActionBar().setTitle("Due Date");
		getActionBar().setGravity(Gravity.CENTER_HORIZONTAL);
        getActionBar().setBackgroundColor(Color.rgb(9, 135, 205));
        getActionBar().getChildAt(0).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ShowDueDateActivity.this,TDAActivity.class);
				startActivity(intent);
			}});
	}
	
	private void initView(){
		findViewById(R.id.dd_overdue_lst).setOnClickListener(this);
		findViewById(R.id.dd_today_list).setOnClickListener(this);
		findViewById(R.id.dd_tomorrow_lst).setOnClickListener(this);
		findViewById(R.id.dd_next7_lst).setOnClickListener(this);
		findViewById(R.id.dd_later_lst).setOnClickListener(this);
		findViewById(R.id.dd_nodue_lst).setOnClickListener(this);
	}
	
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	displaySearchResults();
        	ShowDueDateActivity.this.findViewById(R.id.td_loading).setVisibility(View.GONE);
        	ShowDueDateActivity.this.findViewById(R.id.dd_content).setVisibility(View.VISIBLE);
        }
    };
    
    public void displaySearchResults(){
		((TextView)findViewById(R.id.dd_overdue)).setText(String.valueOf(sumMap.get("overdue")));
		((TextView)findViewById(R.id.dd_today)).setText(String.valueOf(sumMap.get("today")));
		((TextView)findViewById(R.id.dd_tomorrow)).setText(String.valueOf(sumMap.get("tomorrow")));
		((TextView)findViewById(R.id.dd_next7)).setText(String.valueOf(sumMap.get("next7")));
		((TextView)findViewById(R.id.dd_late)).setText(String.valueOf(sumMap.get("later")));
		((TextView)findViewById(R.id.dd_nodue)).setText(String.valueOf(sumMap.get("nodue")));
	}
    
    private Handler eHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(ShowDueDateActivity.this,LoginActivity.class);
        	ShowDueDateActivity.this.startActivity(intent);
        }
    };
    
    private Handler nHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(ShowDueDateActivity.this,TDAActivity.class);
        	ShowDueDateActivity.this.startActivity(intent);
        }
    };
    
    private class SearchThread extends Thread {
        @Override
        public void run() {  
        	try {
				tasks = HttpUtil.getAllTasks(au.getKeyValue());
				Calendar c = Calendar.getInstance();
	    		c.set(Calendar.HOUR, 0);
	    		c.set(Calendar.MINUTE, 0);
	    		c.set(Calendar.SECOND, 0);
	    		int todayTime = Integer.valueOf(CUtil.getStamp(c.getTime()));		
	    		c.setTimeInMillis(c.getTimeInMillis() + 24*3600*1000);
	    		int tomTime = Integer.valueOf(CUtil.getStamp(c.getTime()));		
	    		c.setTimeInMillis(c.getTimeInMillis() + 24*3600*1000);
	    		int tomTime2 = Integer.valueOf(CUtil.getStamp(c.getTime()));		
	    		c.setTimeInMillis(c.getTimeInMillis() + 6*24*3600*1000);
	    		int next7 = Integer.valueOf(CUtil.getStamp(c.getTime()));
	            
	            int noverdue = 0;
	            int ntoday = 0;
	            int ntomorrow = 0;
	            int nnext7 = 0;
	            int nlater = 0;
	            int nnodue = 0;
	        	//get overdue
	    		for(Task t : tasks){
	    			if(t.getDuedate()>0&& t.getDuedate()<todayTime){
	    				noverdue++;
	    			}
	    		}
	    		
	    		//today
	    		for(Task t : tasks){
	    			if(t.getDuedate()<tomTime&& t.getDuedate()>=todayTime){
	    				ntoday++;
	    			}
	    		}
	    		
	    		//tomorrow
	    		for(Task t : tasks){
	    			if(t.getDuedate()<tomTime2&& t.getDuedate()>=tomTime){
	    				ntomorrow++;
	    			}
	    		}
	    		
	    		//next 7 days
	    		for(Task t : tasks){
	    			if(t.getDuedate()<next7&& t.getDuedate()>=todayTime){
	    				nnext7++;
	    			}
	    		}
	    		
	    		//later
	    		for(Task t : tasks){
	    			if(t.getDuedate()>=next7&& t.getDuedate()>=todayTime){
	    				nlater++;
	    			}
	    		}
	    		
	    		//no due date
	    		for(Task t : tasks){
	    			if(t.getDuedate()==0){
	    				nnodue++;
	    			}
	    		}   	

	    		sumMap.put("overdue", noverdue);
	            sumMap.put("today", ntoday);
	            sumMap.put("tomorrow", ntomorrow);
	            sumMap.put("next7", nnext7);
	            sumMap.put("later", nlater);
	            sumMap.put("nodue", nnodue);
	        	
	        	handler.sendEmptyMessage(0);
			} catch (InValidAccountException e) {
				eHandler.sendEmptyMessage(0);
			}catch (Exception e) {
				nHandler.sendEmptyMessage(0);
			}
        	
        	
        }        
    }

}
